package com.sevenwonders;

import com.sevenwonders.util.Helper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

    private static final int MENU_NEW_GAME = Menu.FIRST;
    private static final int MENU_SETTINGS = Menu.FIRST + 1;
    private static final int MENU_ABOUT = Menu.FIRST + 2;
    private static final int MENU_EXIT = Menu.FIRST + 3;

    private static int lockOrientation = -1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
    }

    @Override
    protected void onResume() {
        super.onResume();

        lockOrientation = Helper.updateOrientation(this, lockOrientation);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(Menu.NONE, MENU_NEW_GAME, Menu.NONE, R.string.new_game)
            .setIcon(android.R.drawable.ic_menu_day);
        menu.add(Menu.NONE, MENU_SETTINGS, Menu.NONE, R.string.settings)
            .setIcon(android.R.drawable.ic_menu_preferences);
        menu.add(Menu.NONE, MENU_ABOUT, Menu.NONE, R.string.about)
            .setIcon(android.R.drawable.ic_menu_info_details);
        menu.add(Menu.NONE, MENU_EXIT, Menu.NONE, R.string.exit)
            .setIcon(android.R.drawable.ic_menu_close_clear_cancel);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_NEW_GAME:
            Dialogs.createNewGameDialog(this);
            break;
        case MENU_SETTINGS:
            startActivity(new Intent(this, SettingsActivity.class));
            break;
        case MENU_ABOUT:
            Dialogs.createAboutDialog(this);
            break;
        case MENU_EXIT:
            onDestroy();
            System.exit(0);
            break;
        }
        return true;
    }

    public void onClickNewGame(View view) {
        Dialogs.createNewGameDialog(this);
    }

    public void onClickSettings(View view) {
        startActivity(new Intent(view.getContext(), SettingsActivity.class));
    }

    public void onClickExit(View view) {
        onDestroy();
        System.exit(0);
    }
}
